package com.kitty.game.task.service.targetHandler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.TaskSet;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.model.product.targetparam.DonateTargetParam;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.listener.BaseGameEvent;

import java.util.List;

/**提交给系统 目标管理类*/
public class DonateTargetHandler extends TaskTargetHandler {
    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {

    }

    /**提交给系统*/
    public void donate(Role role) {
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        List<TaskSet> taskSetList = taskService.getAcceptedTasksMatchTarget(role, TaskTargetType.DONATE);

        /**角色当前没有需要东西提交给系统的任务*/
        if (taskSetList.size() == 0) {return ;}

        EquipService equipService = SpringUtils.getBean(EquipService.class);

        for (TaskSet taskSet : taskSetList) {
            TaskTarget roleDoingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);

            /**找不到正在进行任务目标时，跳过*/
            if (roleDoingTaskTarget == null) {continue;}

            DonateTargetParam donateTargetParam = (DonateTargetParam)roleDoingTaskTarget.getTaskTargetParam();

            int realCount = equipService.getCount(donateTargetParam.getItemName(), true, role);
            if (realCount < donateTargetParam.getCount()) {

                continue;
            }

            /**扣除对应数量的物品*/
            equipService.delRoloEquip(donateTargetParam.getItemName(), donateTargetParam.getCount(), true, role);

            taskService.updateTargetProgress(role, taskSet, roleDoingTaskTarget);
        }
    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {

    }
}
